package com.mjh.zhujianju.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.livew.framework.utils.XTCurrentUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.livew.framework.exception.XTBusinessException;
import com.mjh.zhujianju.enums.BanersEnum;
import com.mjh.zhujianju.enums.PermissionEnum;
import com.mjh.zhujianju.model.party.BuildingExamEO;
import com.mjh.zhujianju.service.party.BuildingClassService;
import com.mjh.zhujianju.service.party.BuildingEventSercice;
import com.mjh.zhujianju.service.party.BuildingExamService;
import com.mjh.zhujianju.service.party.BuildingNewsService;
import com.mjh.zhujianju.service.set.BanersService;
import com.mjh.zhujianju.service.set.NavigationService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import com.mjh.zhujianju.vo.party.BuildingClassVO;
import com.mjh.zhujianju.vo.party.BuildingEventVO;
import com.mjh.zhujianju.vo.party.BuildingExamVO;
import com.mjh.zhujianju.vo.party.BuildingNewsVO;
import com.mjh.zhujianju.vo.set.BanersVO;
import com.mjh.zhujianju.vo.set.NavigationVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liuyang
 * @Description: 党建接口
 * @date 2018/8/14 23:57
 */
@RestController
@RequestMapping("/partyBuilding")
@Api("2、WX->党建")
public class PartyBuildingController {

    @Autowired
    private BanersService banersService;

    @Autowired
    private BuildingNewsService buildingNewsService;

    @Autowired
    private BuildingClassService buildingClassService;

    @Autowired
    private BuildingEventSercice buildingEventSercice;

    @Autowired
    private BuildingExamService buildingExamService;

    @Autowired
    private NavigationService navigationService;

    private final String  DEFAULT_TYPE = "0";

    @ApiOperation(value="2-1 首页", notes="展示首页所有信息")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> index (){
        JsonResult result = new JsonResult();
        Map<String,List<Object>> map = Maps.newHashMap();
        try{
            //轮播图
            List<BanersVO> banersEOList = banersService.queryBanersList(BanersEnum.BANERS_SITE_PARTY.getCode());
            map.put("baners", Arrays.asList(banersEOList));

            //菜单
            List<NavigationVO> menus = navigationService.selectNavigationList(PermissionEnum.BANERS_SITE_PARTY.getCode());
            map.put("menu",Arrays.asList(menus));

            //新闻
            List<BuildingNewsVO> buildingNewsVOList = buildingNewsService.queryBuildingNewsIndexList();
            map.put("news",Arrays.asList(buildingNewsVOList));

            //课堂
            List<BuildingClassVO> buildingClassList =  buildingClassService.queryBuildingClassForIndex();
            map.put("classList",Arrays.asList(buildingClassList));

            List<BuildingEventVO> buildingEventVOList = buildingEventSercice.queryBuildingEventIndexList();
            map.put("event",Arrays.asList(buildingEventVOList));

            result.setResult(map);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="2-2 新闻列表", notes="新闻列表")
    @RequestMapping(value = "/news/{type}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "type", value = "新闻类型", required = true, dataType = "String", paramType = "path")
    public ResponseEntity<JsonResult> getNews(@PathVariable(value = "type") String type){
        JsonResult result = new JsonResult();
        try {
            //等于0查询全部
            if (type.equals(DEFAULT_TYPE)){
                type = null;
            }
            List<BuildingNewsVO> list = buildingNewsService.queryBuildingNewsList(type);
            result.setResult(list);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="2-2 新闻-详情", notes="根据公告id查询新闻详情信息")
    @ApiImplicitParam(name = "id", value = "新闻id", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/news/detail/{id}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            BuildingNewsVO buildingNewsVO = buildingNewsService.queryBuildingNewsInfo(id);
            result.setResult(buildingNewsVO);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="2-3 微课堂", notes="微课堂列表")
    @RequestMapping(value = "/class/{type}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "type", value = "课堂类型", required = true, dataType = "String", paramType = "path")
    public ResponseEntity<JsonResult> getClass(@PathVariable(value = "type") String type){
        JsonResult result = new JsonResult();
        try {
            //等于0查询全部
            if (type.equals(DEFAULT_TYPE)){
                type = null;
            }
            List<BuildingClassVO> list= buildingClassService.queryBuildingClassList(type);
            result.setResult(list);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }


    @ApiOperation(value="2-3 微课堂->详情", notes="根据微课堂id查询详细信息")
    @RequestMapping(value = "/class/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "微课堂id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getRemoveInfo (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            BuildingClassVO buildingClassVO = buildingClassService.queryBuildingClassInfo(id,userId);
            result.setResult(buildingClassVO);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }


    @ApiOperation(value="2-4 活动", notes="活动列表")
    @RequestMapping(value = "/event/{type}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getEvent(@PathVariable(value = "type") String type){
        JsonResult result = new JsonResult();
        try {
            List<BuildingEventVO> list = buildingEventSercice.queryBuildingEventListIndex(type);
            result.setResult(list);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="活动详情", notes="查询活动详情")
    @RequestMapping(value = "/event/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "活动id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getEventInfo(@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            BuildingEventVO vo = buildingEventSercice.queryBuildingEventInfo(id);
            result.setResult(vo);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }


    @ApiOperation(value="2-5 在线考试", notes="在线考试列表")
    @RequestMapping(value = "/exam", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getExam(){
        JsonResult result = new JsonResult();
        try {
            List<BuildingExamVO> map = buildingExamService.queryBuildingExamForWXList();
            result.setResult(map);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="2-5 在线考试->详情", notes="在线考试详情")
    @RequestMapping(value = "/exam/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "在线考试id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getExamInfo(@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            BuildingExamEO buildingExam  = buildingExamService.getByPrimaryKey(id);
            BuildingExamVO vo = new BuildingExamVO();
            BeanUtils.copyProperties(buildingExam,vo);
            
            if(vo.getEndTime() != null && vo.getStartTime() != null) {
                long diff = (vo.getEndTime().getTime()- vo.getStartTime().getTime());
                diff = diff / (1000 * 60);
                vo.setTimeInterval(diff);
            }
            result.setResult(vo);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }


}
